package com.soup.view;

import com.soup.dao.PersonDao;
import com.soup.domain.Person;
import com.soup.utils.StringUtils;
import com.soup.view.component.Context;
import com.soup.view.component.MassageTip;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PersonForm extends Context implements ActionListener {


    private JTextField id = new JTextField(10);
    private JTextField name = new JTextField(10);
    private JPasswordField pass = new JPasswordField(10);
    private JTextField ind = new JTextField(10);
    private JTextField money = new JTextField(10);

    private JButton add = new JButton("确认注册");

    private PersonDao dao = new PersonDao();

    public PersonForm(int width,int height) {
        super(width, height);
        id.setBounds(60,0,width-120,48);
        name.setBounds(60,50,width-120,48);
        pass.setBounds(60,100,width-120,48);
        ind.setBounds(60,150,width-120,48);
        money.setBounds(60,200,width-120,48);
        this.add(id,"账号",100);
        this.add(name,"姓名",100);
        this.add(pass,"密码",100);
        this.add(ind,"身份",100);
        this.add(money,"存钱",100);

        id.setText("账号长度必须为9位的数字");
        name.setText("你的真实姓名");
        ind.setText("只能填‘管理员’或者‘学生’");
        money.setText("账户余额");

        pass.setEchoChar('*');

        add.setBounds(60,340,width-120,48);
        this.add(add,20);
        add.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == add){

            if (!StringUtils.isEmpty(id.getText()) && id.getText().length() == 9){

                if (!StringUtils.isEmpty(name.getText())){

                    if (!StringUtils.isEmpty(new String(pass.getPassword()))){

                        if (ind.getText().equals("学生") || ind.getText().equals("管理员")){
                            Person person = new Person();
                            person.setId(id.getText());
                            person.setName(name.getText());
                            person.setPassword(new String(pass.getPassword()));
                            person.setInd(ind.getText());
                            person.setState("正常");
                            if (!StringUtils.isEmpty(money.getText())){
                                person.setMoney(Integer.parseInt(money.getText()));
                            }else{
                                person.setMoney(0);
                            }
                            int result = dao.addPerson(person);
                            if (result > 0){
                                MassageTip.showMassage(null,"注册成功");
                            }else{
                                MassageTip.showMassage(null,"注册失败");
                            }
                        }else{
                            MassageTip.showMassage(null,"身份只能是学生或管理员");
                        }

                    }else{
                        MassageTip.showMassage(null,"密码不能为空");
                    }

                }else{
                    MassageTip.showMassage(null,"名称不能为空");
                }

            }else {
                MassageTip.showMassage(null,"学号格式不正确");
            }

        }
    }
}